#include "card.h"

QHash<QString, int> Card::cardNoCmp = QHash<QString, int>();

Card Card::parseString(QString card)
{
    QStringList sl = card.split('|');
    return Card(sl[0], sl[1]);
}

Card::Card(QString cardNo, QString cardColor):
    cardNo(cardNo), cardColor(cardColor)
{
    if(cardNoCmp.isEmpty()){
        cardNoCmp.insert("2", 13);
        cardNoCmp.insert("A", 12);
        cardNoCmp.insert("K", 11);
        cardNoCmp.insert("Q", 10);
        cardNoCmp.insert("J", 9);
        for(int i=3; i<=10; i++){
            cardNoCmp.insert(QString("%1").arg(i), i-2);
        }
    }
}

bool Card::operator<(const Card &other)
{
    if(cardNo == "JOKER-A") return false;
    if(other.cardNo == "JOKER-A") return true;
    if(cardNo == "JOKER-B") return false;
    if(other.cardNo == "JOKER-B") return true;

    // 先比较序号再比较花色
    int val1 = cardNoCmp[cardNo];
    int val2 = cardNoCmp[other.cardNo];
    if(val1 == val2){
        // 比较花色
        if(cardColor == "Heart") return false;
        if(other.cardColor == "Heart") return true;
        if(cardColor == "Spade") return false;
        if(other.cardColor == "Spade") return true;
        if(cardColor == "Club") return false;
        else return true;
    }
    return val1 < val2;
}
